class Solution {
        int m, n;
            int dx[4] = {0, 0, 1, -1};
                int dy[4] = {1, -1, 0, 0};
                    typedef pair<int, int> PII;

    public:
                    int nearestExit(vector<vector<char>>& maze, vector<int>& e) {
                                m = maze.size(), n = maze[0].size();
                                        queue<PII> q;
                                                vector<vector<bool>> vis(m, vector<bool>(n, false));
                                                        q.push({e[0], e[1]});
                                                                vis[e[0]][e[1]] = true;

                                                                        int ret = 0;
                                                                                while (q.size())
                                                                                {
                                                                                                ret++;

                                                                                                            int sz = q.size();
                                                                                                                        while (sz--)
                                                                                                                        {
                                                                                                                                            auto [a, b] = q.front();
                                                                                                                                                            q.pop(); 
                                                                                                                                                                            for (int i = 0; i < 4; i++)
                                                                                                                                                                            {
                                                                                                                                                                                                    int x = a + dx[i], y = b + dy[i];
                                                                                                                                                                                                    if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' && !vis[x][y]) {
                                                                                                                                                                                                                                if (x == 0 || x == m - 1 || y == 0 || y == n - 1)
                                                                                                                                                                                                                                                                return ret; 
                                                                                                                                                                                                                                                        q.push({x, y});
                                                                                                                                                                                                                                                                                vis[x][y] = true;
                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                    }
                                                                                                                                                                                                                    
                                                                                                                                                                            }
                                                                                                                                                                                        
                                                                                                                        }
                                                                                                                                
                                                                                }
                                                                                        return -1;
                                                                                            
                    }

};
